import 'package:flutter/material.dart';
import 'package:image_cropper/image_cropper.dart';

class XrImageCropper {
  /// 裁剪
  static Future<CroppedFile?> onCropperImage(
      {required BuildContext context,
      required String sourcePath,
      required bool aspectRatioLockEnabled,
      required bool defaultAspectRatio}) async {
    try {
      final _croppedFile = await ImageCropper().cropImage(
        sourcePath: sourcePath,
        aspectRatio: defaultAspectRatio ? CropAspectRatio(ratioX: 1, ratioY: 1) : null,
        aspectRatioPresets: [
          CropAspectRatioPreset.original,
          // CropAspectRatioPreset.square,
          // CropAspectRatioPreset.ratio3x2,
          // CropAspectRatioPreset.ratio4x3,
          // CropAspectRatioPreset.ratio16x9
        ],
        compressFormat: ImageCompressFormat.png,
        compressQuality: 90,
        uiSettings: [
          AndroidUiSettings(
              toolbarTitle: '',
              toolbarColor: Colors.black,
              toolbarWidgetColor: Colors.white,

              /// 初始裁剪比例
              initAspectRatio: CropAspectRatioPreset.original,
              lockAspectRatio: aspectRatioLockEnabled,
              hideBottomControls: true),
          IOSUiSettings(
            title: '',
            aspectRatioLockEnabled: aspectRatioLockEnabled,
            aspectRatioPickerButtonHidden: true,
            rotateButtonsHidden: true,
            resetButtonHidden: true,
          ),
        ],
      );
      return Future.value(_croppedFile);
    } catch (e) {
      return Future.error(e);
    }
  }
}
